﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WMM.Tapioca.Utilities;

namespace WMM.Tapioca.Linkbase
{
    /// <summary>
    /// XBRL 2.1 Specification 3.5.2.4
    /// The roleRef element is used to resolve custom xlink:role values that are
    /// used in a linkbase or XBRL instance (for footnoteLink and footnote). 
    /// </summary>
    [Serializable]
    public class RoleRef
    {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        internal string href;
        internal string schemaUri;
        internal string id;
        internal string roleUri;

        private XbrlDocument parentDocument;

        /// <summary>
        /// XbrlDocument parent.
        /// </summary>
        internal XbrlDocument ParentDocument
        {
            get { return parentDocument; }
            set { parentDocument = value; }
        }


        /// <summary>
        /// Creates a new instance of <see cref="RoleRef"/>.
        /// </summary>
        public RoleRef()
        {
        }

        /// <summary>
        /// Overloaded.  Creates a new instance of <see cref="RoleRef"/>.
        /// </summary>
        /// <param name="document">The parent document.</param>
        /// <param name="hrefArg">The href property value to be assigned 
        /// to the newly created <see cref="RoleRef"/>.</param>
        /// <param name="uriArg">The uri property value to be assigned 
        /// to the newly created <see cref="RoleRef"/>.</param>
        public RoleRef(XbrlDocument document, string hrefArg, string uriArg)
        {
            log.Debug("Creating ArcRoleRef: " + uriArg + "-" + hrefArg);

            href = hrefArg;
            schemaUri = GetSchemaName();
            id = GetId();
            roleUri = uriArg;
            parentDocument = document;
        }


        /// <summary>
        /// Retrieves the schema portion of this <see cref="RoleRef"/>'s href property.
        /// </summary>
        /// <example>
        /// If href = "balancesheet.xsd#assets", GetSchemaName = "balancesheet.xsd".
        /// </example>
        /// <returns>The parsed schema.</returns>
        /// <exception cref="ApplicationException">If the href is not well formed.</exception>
        /// <exception cref="ArgumentNullException">If the href is null.</exception>
        internal string GetSchemaName()
        {
            string[] strs = href.Split('#');

            if (strs.Length < 2)
            {
                throw new XbrlException("Can't split href: " + href);
            }

            return strs[0];
        }

        /// <summary>
        /// Retrieves the id portion of this <see cref="RoleRef"/>'s href property.
        /// </summary>
        /// <example>
        /// If href = "balancesheet.xsd#assets", GetSchemaName = "assets".
        /// </example>
        /// <exception cref="ApplicationException">If the href is not well formed.</exception>
        /// <exception cref="ArgumentNullException">If the href is null.</exception>
        public string GetId()
        {
            string[] strs = href.Split('#');

            if (strs.Length < 2)
            {
                throw new XbrlException("Can't split href: " + href);
            }

            return strs[1];
        }

        internal string GetHref()
        {
            return href;
        }

        internal void SetHref(string s)
        {
            href = s;
        }

    }
}
